package cn.stylefeng.guns.modular.demand.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.demand.entity.Demand;
import cn.stylefeng.guns.modular.demand.mapper.DemandMapper;
import cn.stylefeng.guns.modular.demand.model.params.DemandParam;
import cn.stylefeng.guns.modular.demand.model.result.DemandResult;
import cn.stylefeng.guns.modular.demand.service.DemandService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 需求表 服务实现类
 * </p>
 *
 * @author jalor
 * @since 2019-10-20
 */
@Service
public class DemandServiceImpl extends ServiceImpl<DemandMapper, Demand> implements DemandService {

    @Override
    public void add(DemandParam param) {
        Demand entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DemandParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(DemandParam param) {
        Demand oldEntity = getOldEntity(param);
        Demand newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DemandResult findBySpec(DemandParam param) {
        return null;
    }

    @Override
    public List<DemandResult> findListBySpec(DemandParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DemandParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 获取所有需求
     *
     * @param productId 产品ID
     * @param featureId 功能ID
     * @param code      编号
     * @param name      名称
     * @return 需求集合
     */
    @Override
    public List<Demand> listAll(String productId, String featureId, String code, String name) {
        return this.baseMapper.listAll(productId, featureId, code, name);
    }

    private Serializable getKey(DemandParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Demand getOldEntity(DemandParam param) {
        return this.getById(getKey(param));
    }

    private Demand getEntity(DemandParam param) {
        Demand entity = new Demand();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
